package com.huawei.hms.framework.network.restclient.hwhttp;

import com.huawei.hms.framework.common.Logger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadFactory;

public final class RequestThreadPoolManager {
    private static final String TAG = "RequestThreadPoolMgr";
    private ExecutorService threadPoolExecutor;

    static class MyThreadFactory implements ThreadFactory {
        MyThreadFactory() {
        }

        public Thread newThread(Runnable runnable) {
            return new Thread(runnable, "HttpsBizThreadPool");
        }
    }

    static class Singletone {
        public static final RequestThreadPoolManager instance = new RequestThreadPoolManager();

        private Singletone() {
        }
    }

    private RequestThreadPoolManager() {
        this.threadPoolExecutor = null;
        Logger.i(TAG, "ThreadPool init!");
        this.threadPoolExecutor = Executors.newCachedThreadPool(new MyThreadFactory());
    }

    public static RequestThreadPoolManager getInstance() {
        return Singletone.instance;
    }

    public void execute(Runnable runnable) {
        try {
            this.threadPoolExecutor.execute(runnable);
        } catch (RejectedExecutionException e) {
            Logger.e(TAG, "the runnable task cannot be accepted for execution");
        }
    }

    public void release() {
        Logger.i(TAG, "ThreadPool release!");
        if (this.threadPoolExecutor != null && !this.threadPoolExecutor.isShutdown()) {
            this.threadPoolExecutor.shutdown();
        }
    }

    public Future submit(Runnable runnable) {
        Future future = null;
        try {
            future = this.threadPoolExecutor.submit(runnable);
        } catch (RejectedExecutionException e) {
            Logger.e(TAG, "the runnable task cannot be accepted for execution");
        }
        return future;
    }
}
